/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trim.gui.testing.models;

import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import trim.common.Format;
import trim.common.inputdata.FormatOrder;
import trim.common.inputdata.InputData;

/**
 *
 * @author Дмитрий
 */
public class RequestFormatsModel extends AbstractTableModel
        implements IRowManipulationTableModel,TableModelListener
{

  List<FormatOrder> formatCountList;
  boolean editable=true;

  public RequestFormatsModel(InputData inputData)
  {
    this.formatCountList=inputData.getFormatOrderList();
  }

  public RequestFormatsModel(List<FormatOrder> formatCountList,boolean editable)
  {
    this.formatCountList=formatCountList;
    this.editable=editable;
  }

  @Override
  public String getColumnName(int column)
  {
    switch(column){
      case 0:
        return "Format";
      case 1:
        return "Count";
    }
    return super.getColumnName(column);
  }

  @Override
  public Class<?> getColumnClass(int columnIndex)
  {
    return Integer.class;
  }

  public int getRowCount()
  {
    return formatCountList.size();
  }

  public int getColumnCount()
  {
    return 2;
  }

  public Object getValueAt(int rowIndex,int columnIndex)
  {
    switch(columnIndex){
      case 0:
        return formatCountList.get(rowIndex).getFormat().getLength();
      case 1:
        return formatCountList.get(rowIndex).getCount();
    }
    return 0;
  }

  @Override
  public boolean isCellEditable(int row,int col)
  {
    return editable&&col==1;
  }

  @Override
  public void setValueAt(Object aValue,int rowIndex,int columnIndex)
  {
    switch(columnIndex){
      //case 0:getFormats().set(rowIndex,Integer.parseInt(aValue.toString()));break;
      case 1:
        formatCountList.get(rowIndex).setCount(Integer.parseInt(aValue.toString()));
        break;
    }
    //fireTableDataChanged();
    fireTableRowsUpdated(rowIndex,rowIndex);
  }

  public void addRow()
  {
    formatCountList.add(new FormatOrder(new Format(0),0));
    fireTableRowsInserted(formatCountList.size()-1,formatCountList.size()-1);
  }

  public void removeRow(int i)
  {
    formatCountList.remove(i);
    fireTableRowsDeleted(i,i);
  }

  public void insertRow(int r)
  {
    formatCountList.add(r,new FormatOrder(new Format(0),0));
    fireTableRowsInserted(r,r);
  }

  public void tableChanged(TableModelEvent e)
  {
    fireTableDataChanged();
  }

  public Object getObject(int r)
  {
    return formatCountList.get(r).getFormat();
  }
}
